package com.aring.controller;


import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.aring.bean.MatchInfo;
import com.aring.bean.Place;
import com.aring.bean.PlaceManager;
import com.aring.bean.School;
import com.aring.bean.TimeTable;
import com.aring.config.FileServerConfig;
import com.aring.dao.SchoolDao;
import com.aring.service.PlaceManagerService;
import com.aring.utils.FileProcessor;



@Controller
public class PlaceManagerController {
	@Autowired
	private PlaceManagerService ManagerService;
	@Autowired
	private FileServerConfig fileServerConfig;
	@RequestMapping(value="/ManagerRegister.do",method=RequestMethod.POST)
	public ModelAndView ManagerRegister(PlaceManager manager){
		ModelAndView mv=new ModelAndView("error");
		System.out.println(manager.toString());
		manager.setStatus(0);
		if(ManagerService.addPlaceManager(manager)==1){
			mv.addObject("message","待审核！");
			
			mv.setViewName("redirect:/PlaceManager/ManagerLogin.jsp");
			
		}
	    return mv;	
	}
	@RequestMapping(value="/ManagerLogin.do", method=RequestMethod.POST)
	public ModelAndView ManagerLogin(String account,String password,HttpServletResponse response){
		PlaceManager pm;
		ModelAndView mv =new ModelAndView("redirect:/index.jsp");
		pm=ManagerService.selectPlaceManagerByAccount(account);
		if(pm==null||!(pm.getPassword().trim()).equals(password)) {mv.addObject("message", "账号或密码错误");  mv.setViewName("error");return mv;}
		if(pm.getStatus()==0) { mv.addObject("message", "待审核!");mv.setViewName("error");return mv;}
		if((pm.getPassword().trim()).equals(password))
		{
			Cookie cookie=new Cookie("judge","assfgd");
			cookie.setValue(pm.getPm_no()+"");
			response.addCookie(cookie);
			mv.setViewName("redirect:/PlaceManager/placeManager.jsp");
			return mv;}
		return mv;
		
	}
	//用于判断是否登陆
	public String judgeLogin(HttpServletRequest request){
		Cookie[] c=request.getCookies();
		String judge=null;
		for(int i=0;i<c.length;i++){
			if(c[i].getName().equals("judge")) judge=c[i].getValue();
		}
		return judge;
	}
	@RequestMapping(value="/PublishMatch.do" ,method=RequestMethod.POST)
	public ModelAndView PublishMatch(String content ,String match_time,String place,HttpServletRequest request )
	{
		ModelAndView mv =new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
		String judge=judgeLogin(request);
		if(judge==null) return mv;
	    MatchInfo mi=new MatchInfo();
	    mi.setContent(content);
	    Timestamp time = new Timestamp(System.currentTimeMillis());
	    System.out.print(match_time);
	    mi.setMatch_time(Timestamp.valueOf(match_time+" 00:00:00"));
	    mi.setPlace(place);
	    mi.setPublic_time(time);
	    mi.setPm_no(Integer.valueOf(judge));
	    System.out.println(mi.getPm_no()+"<----------------");
	    if(ManagerService.insertMatchInfo(mi)==1) {mv.setViewName("redirect:/PlaceManager/placeManager.jsp");return mv;}
		 { mv.addObject("message", "发布失败");mv.setViewName("redirect:/PlaceManager/publishMatch.jsp"); return mv;}
	}
    @RequestMapping(value="/BeforeEditManager.do" )
    public ModelAndView BeforeEditManager(HttpServletRequest request){
    	ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	PlaceManager manager=ManagerService.selectPlaceManagerByPm_no(Integer.valueOf(judge));
    	request.getSession().setAttribute("manager", manager);
    	mv.setViewName("redirect:/PlaceManager/editManager.jsp");
    	return mv;
    }
    
    @RequestMapping(value="/editManager.do",method=RequestMethod.POST)
    public ModelAndView EditManager(HttpServletRequest request,String account,String password,String schoolId,String age){
    	
    	ModelAndView mv=new ModelAndView("redirect:/PlaceManager/EditManager.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	PlaceManager manager=ManagerService.selectPlaceManagerByAccount(account);
    	manager.setAge(Integer.valueOf(age));
    	manager.setPassword(password);
    	manager.setAccount(account);
    	manager.setSchoolId(Integer.valueOf(schoolId));
    	if(1==ManagerService.updateManager(manager)) mv.setViewName("redirect:/PlaceManager/placeManager.jsp");
    	else mv.setViewName("error");
    	return mv;
    	
    }
    @RequestMapping(value="/selectMatchInfo")
    public ModelAndView selectMatchInfo(HttpServletRequest request){
    	ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	ArrayList<PlaceManager> list=new ArrayList<PlaceManager>();
    	list=ManagerService.selectMatchInfo(Integer.valueOf(judge));
    	request.getSession().setAttribute("list", list);
    	mv.setViewName("redirect:/PlaceManager/showMatch.jsp");
    	return mv;
    }
    
    @RequestMapping(value="/outLogin.do")
    public String outLogin(HttpServletRequest request,HttpServletResponse response)
    {
    	Cookie[] cookies=request.getCookies();
    	if(cookies==null) return "redirect:/PlaceManager/ManagerLogin.jsp";
    	for (int i = 0; i < cookies.length; i++) {
		  cookies[i]=new Cookie(cookies[i].getName(),null);
		  cookies[i].setMaxAge(0);
		  response.addCookie(cookies[i]);
		}
    	return "redirect:/PlaceManager/ManagerLogin.jsp";
    }
    
    @RequestMapping(value="/editMatch.do")
    public ModelAndView editMatch(HttpServletRequest request,String match_no){
    	ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	MatchInfo match=ManagerService.selectMatchByMi_no(Integer.valueOf(match_no));
    	request.getSession().setAttribute("match", match);
    	mv.setViewName("redirect:/PlaceManager/editMatch.jsp");
    	return mv;
    }
    @RequestMapping(value="/updateMatch.do" ,method=RequestMethod.POST)
    public ModelAndView updateMatch(HttpServletRequest request,String mi_no,String place,String content,String match_time){
    	ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	MatchInfo match=new MatchInfo();
    	match.setMi_no(Integer.valueOf(mi_no));
    	match.setPlace(place);
    	match.setContent(content);
    	match.setMatch_time(Timestamp.valueOf(match_time+" 00:00:00"));
    	match.setPm_no(Integer.valueOf(judge));
    	Timestamp time=new Timestamp(System.currentTimeMillis());
    	match.setPublic_time(time);
    	if(1==ManagerService.updateMatch(match))
    		mv.setViewName("redirect:/PlaceManager/placeManager.jsp");
    	else mv.setViewName("error");
    	return mv;
    }
    
	
	@RequestMapping(value="/publishPlace.do",method=RequestMethod.POST)
	public  ModelAndView image(@RequestParam("file") MultipartFile file,String type,String content,String schoolId,HttpServletRequest request){
		ModelAndView mView=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mView;
		try{
		String Itype = file.getContentType();
		Itype = Itype.split("/")[0];
		String name = FileProcessor.save(fileServerConfig.getImagePath(), file);
		String url = "http://"+fileServerConfig.getHost()+":"+fileServerConfig.getPort()+"/images/"+name;
		mView.setViewName("success");
		mView.addObject("message", "文件上传成功");
		mView.addObject("next", url);
		System.out.println(url);
		Place place=new Place();
		place.setImage_url(url);
		place.setPm_no(Integer.valueOf(judge));
		place.setSchoolId(Integer.valueOf(schoolId));
		place.setContent(content);
		place.setType(type);
		if(1==ManagerService.insertPlace(place))
		mView.setViewName("redirect:/PlaceManager/placeManager.jsp");
		else mView.setViewName("error");
		return mView;
		}catch(Exception e){
			e.printStackTrace();
			mView.addObject("message", e.getMessage());
			return mView;
		}
	}
	@RequestMapping(value="/selectPlace.do" )
	public ModelAndView selectPlace(HttpServletRequest request){
		ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	ArrayList<Place> list=new ArrayList<Place>();
    	list=ManagerService.selectPlaceByPm_no(Integer.valueOf(judge));
    	request.getSession().setAttribute("list", list);
    	mv.setViewName("redirect:/PlaceManager/showPlace.jsp");
    	return mv;
		
	}
	
	@RequestMapping(value="/editPlace.do")
	public ModelAndView editPlace(HttpServletRequest request,String place_no){
		ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	Place place=ManagerService.selectPlaceByP_no(Integer.valueOf(place_no));
    	request.getSession().setAttribute("place", place);
    	mv.setViewName("redirect:/PlaceManager/editPlace.jsp");
    	return mv;
		
	}
	
	@RequestMapping(value="/updatePlace.do",method=RequestMethod.POST)
	public  ModelAndView updatePlace(@RequestParam("file") MultipartFile file,String type,String content,String schoolId,HttpServletRequest request,String place_no){
		ModelAndView mView=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	String url=null;
    	if(judge==null) return mView;
    	Place place=ManagerService.selectPlaceByP_no(Integer.valueOf(place_no));
		try{
		if(file!=null){
		String Itype = file.getContentType();
		Itype = Itype.split("/")[0];
		String name = FileProcessor.save(fileServerConfig.getImagePath(), file);
		 url= "http://"+fileServerConfig.getHost()+":"+fileServerConfig.getPort()+"/images/"+name;
		mView.setViewName("success");
		mView.addObject("message", "文件上传成功");
		mView.addObject("next", url);
		System.out.println(url);
		}
		else url=place.getImage_url();
		place.setImage_url(url);
		place.setSchoolId(Integer.valueOf(schoolId));
		place.setContent(content);
		place.setType(type);
		if(1==ManagerService.updatePlace(place))
		mView.setViewName("redirect:/PlaceManager/placeManager.jsp");
		else mView.setViewName("error");
		return mView;
		}catch(Exception e){
			e.printStackTrace();
			mView.addObject("message", e.getMessage());
			return mView;
		}
	}
	@RequestMapping(value="/beforeAddNum.do")
	public ModelAndView BeforeAddNum(HttpServletRequest request){
		ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	ArrayList<School> list=ManagerService.querySchool(Integer.valueOf(judge));
    	request.getSession().setAttribute("school", list);
    	mv.setViewName("redirect:/PlaceManager/selectSchool.jsp");
    	return mv;
    	
		
	}
	@RequestMapping(value="/selectType.do",method=RequestMethod.POST)
	public ModelAndView selectType(HttpServletRequest request,String school)
	{
		ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	ArrayList type=ManagerService.queryType(Integer.valueOf(judge),Integer.valueOf(school));
        School s=ManagerService.querySchoolById(Integer.valueOf(school));
    	request.getSession().setAttribute("list", type);
    	request.getSession().setAttribute("school", s);
    	mv.setViewName("redirect:/PlaceManager/addPlaceNum.jsp");
    	return mv;
		
	}
	
	@RequestMapping(value="/addPlaceNum.do",method=RequestMethod.POST)
	public ModelAndView addPlaceNum(HttpServletRequest request,String schoolName,String time,String day,String rent,String Type,String num){
		ModelAndView mv=new ModelAndView("redirect:/PlaceManager/ManagerLogin.jsp");
    	String judge=judgeLogin(request);
    	if(judge==null) return mv;
    	Integer p_no=ManagerService.queryP_noBySchoolAndType(schoolName,Type);
    	Integer pn_id=null;
    	if(ManagerService.queryPn_id(p_no,Integer.valueOf(num))==null){
    	if(1!=ManagerService.insertPlaceNum(p_no,Integer.valueOf(num))){
    		mv.setViewName("error");
    		return mv;
    		
    	}}
    	pn_id=ManagerService.queryPn_id(p_no,Integer.valueOf(num));
    	TimeTable table=new TimeTable();
		table.setPlaceNumberId(pn_id);
		table.setRent(Integer.valueOf(rent));
		table.setStatus(0);
		table.setWeekday(day);
		table.setTime(time);
		if(1==ManagerService.insertTimeTable(table))
		{
			mv.setViewName("redirect:/PlaceManager/placeManager.jsp");
			return mv;
		}
    	 mv.setViewName("error");
    	 mv.addObject("message", "插入错误");
    	 return mv;
		
	}
	
}
